/***************
**
*
*公共JS
*
*/

var appSecret = 'b4fd9dc55b17a580f6bad51f277438a2';
var appkey = 'yqb_web';

var layer_offset = '5px';
var layer_height = '97%';

$(function(){

	$(".modal-header .close").text('');//删除关闭

});
	//请求获取数据
	function http_to_request(url,parameter=''){
		var version = $('input[name="version"]').val();
		var imei = $('input[name="imei"]').val();
		var session = $('input[name="session"]').val();
		parameter += '&version='+version+'&imei='+imei+'&session='+session;

		http_data = url_to_json(parameter);			//参数转json
		http_data = encodeURIComponent(http_data);	//转码

		var http_time = (new Date()).valueOf();		//时间戳
		md5_data = $.md5(http_data);		//参数加密

		var checksum = hex_sha1(appSecret+md5_data+http_time);		//获取加密串
		formdata = 'http_data='+http_data+'&checksum='+checksum+'&http_time='+http_time+'&AppKey='+appkey;	//拼接请求参数

		var result = '';
		layer_load();//加载层

		$.ajax({
			type : "post",
			url : url,
			data : formdata,
			dataType : "html",
			async: false, //同步传输，并添加返回值，返回值应为已定义的全局变量 如a
			success : function(data) {
				var json_data = isJsonFormat(data);
				if(json_data){
					json_data = $.parseJSON(data);
					if(json_data.code != 200){
						if(json_data.code == 'T200'){
							layer.msg(json_data.message, {icon: 1});
						}else{
							layer.msg(json_data.message, {icon: 2});
							layer.closeAll('loading'); //关闭加载层
							throw "";
						}

					}

				}else{
					result = data;

				}
			}
		});

		return result;
	}

	//返回加密后的请求地址
	function http_to_request_url(url,parameter=''){
		var version = $('input[name="version"]').val();
		var imei = $('input[name="imei"]').val();
		var session = $('input[name="session"]').val();
		parameter += '&version='+version+'&imei='+imei+'&session='+session;

		http_data = url_to_json(parameter);			//参数转json
		http_data = encodeURIComponent(http_data);	//转码

		var http_time = (new Date()).valueOf();		//时间戳
		md5_data = $.md5(http_data);		//参数加密

		var checksum = hex_sha1(appSecret+md5_data+http_time);		//获取加密串
		formdata = 'http_data='+http_data+'&checksum='+checksum+'&http_time='+http_time+'&AppKey='+appkey;	//拼接请求参数

		return url+'?'+formdata;
	}

	//判断字符串是否为json
	function isJsonFormat( str ) {
		try{
			$.parseJSON(str);
		}catch(e){
			return false;
		}
		return true;
	}


	//参数转json
	function url_to_json(url){
		var data = url.split('&');
		var json = '{';
		$.each(data,function(k,v){
			var data_n = v.split('=');
			json += '"'+data_n[0]+'":"'+data_n[1]+'",';
		})

		json =  json.substring(0,json.length-1);
		json += '}';
		console.log(json);
		return json;
	}

	
	//加载层
	function layer_load(){
		layer.load(2, {content:'加载中。。。',shade: [0.6, '#393D49'],success: function(layero){
			layero.find('.layui-layer-content').css('width', '300px').css('color', '#999').css('font-size', '26px').css('padding-left', '40px');//.css('background', 'none')
		}});
	}

	//zui单选返回选中状态
	function status_selected(class_name,value){
		//营业状态选中
		$(class_name+" option[value='"+value+"']").attr("selected",true);
		$(class_name).on('chosen:ready', function(e, params) {
		  $(class_name).val(value)//设置值
		});
		$(class_name).trigger('chosen:updated');//更新选项
	}
